#region License

// This file is part of Grawlix.
// 
// Grawlix is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Grawlix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public
// License along with Grawlix.  If not, see
// <http://www.gnu.org/licenses/>.

#endregion

using System;
using System.Threading;

namespace Grawlix.Concurrency
{
	/// <summary>
	/// A <see cref="Future"/> object that has to be manually started.
	/// </summary>
	public class DelayedFuture : Future
	{
		/// <summary>
		/// Whether the future has started running yet.
		/// </summary>
		private int _hasStarted;

		/// <summary>
		/// Gets a value indicating whether the future has started running.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this future has started running; otherwise, 
		/// 	<c>false</c>.
		/// </value>
		public bool HasStarted
		{
			get
			{
				Thread.MemoryBarrier();
				return _hasStarted != 0;
			}
		}

		/// <summary>
		/// Runs the specified action on the thread pool.
		/// </summary>
		/// <param name="action">The action to run.</param>
		/// <exception cref="InvalidOperationException">The delayed
		/// future object has already been executed.</exception>
		public void Execute(Action action)
		{
			Execute(new ThreadPoolExecutor(), action);
		}

		/// <summary>
		/// Runs the specified action on the specified executor.
		/// </summary>
		/// <param name="executor">The executor used to run the action.
		/// </param>
		/// <param name="action">The action to run.</param>
		/// <exception cref="InvalidOperationException">The delayed
		/// future object has already been executed.</exception>
		public void Execute(IExecutor executor, Action action)
		{
			int result = Interlocked.Exchange(ref _hasStarted, 1);
			if (result == 0)
				Run(executor, action);
			else
			{
				throw new InvalidOperationException(
					"The delayed future object has already been executed.");
			}
		}
	}

	/// <summary>
	/// A <see cref="Future{T}"/> object that has to be manually started.
	/// </summary>
	/// <typeparam name="T">The result of the operation.</typeparam>
	internal class DelayedFuture<T> : Future<T>
	{
		/// <summary>
		/// Whether the future has started running yet.
		/// </summary>
		private int _hasStarted;

		/// <summary>
		/// Gets a value indicating whether the future has started running.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this future has started running; otherwise, 
		/// 	<c>false</c>.
		/// </value>
		public bool HasStarted
		{
			get
			{
				Thread.MemoryBarrier();
				return _hasStarted != 0;
			}
		}

		/// <summary>
		/// Runs the specified action on the thread pool.
		/// </summary>
		/// <param name="action">The action to run.</param>
		/// <exception cref="InvalidOperationException">The delayed
		/// future object has already been executed.</exception>
		public void Execute(Func<T> action)
		{
			Execute(new ThreadPoolExecutor(), action);
		}

		/// <summary>
		/// Runs the specified action on the specified executor.
		/// </summary>
		/// <param name="executor">The executor used to run the action.
		/// </param>
		/// <param name="action">The action to run.</param>
		/// <exception cref="InvalidOperationException">The delayed
		/// future object has already been executed.</exception>
		public void Execute(IExecutor executor, Func<T> action)
		{
			int result = Interlocked.Exchange(ref _hasStarted, 1);
			if (result == 0)
				Run(executor, action);
			else
			{
				throw new InvalidOperationException(
					"The delayed future object has already been executed.");
			}
		}
	}
}